#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "rt_list.h"
#include "jhash.h"

typedef struct testlist
{
    int num;
    char* str;
    struct list_head list;
}testlist_s;
testlist_s ele1, ele2, ele3, ele4, ele5;
testlist_s elea, eleb, elec;

void element_init(void)
{
    INIT_LIST_HEAD(&ele1.list);//初始化结构体中的链表指针
    ele1.num = 1;
    ele1.str = "NO. 1";
    INIT_LIST_HEAD(&ele2.list);//初始化结构体中的链表指针
    ele2.num = 2;
    ele2.str = "NO. 2";
    INIT_LIST_HEAD(&ele3.list);//初始化结构体中的链表指针
    ele3.num = 3;
    ele3.str = "NO. 3";
    INIT_LIST_HEAD(&ele4.list);//初始化结构体中的链表指针
    ele4.num = 4;
    ele4.str = "NO. 4";
    INIT_LIST_HEAD(&ele5.list);//初始化结构体中的链表指针
    ele5.num = 5;
    ele5.str = "NO. 5";

    INIT_LIST_HEAD(&elea.list);//初始化结构体中的链表指针
    elea.num = 1;
    elea.str = "NO. a";
    INIT_LIST_HEAD(&eleb.list);//初始化结构体中的链表指针
    eleb.num = 2;
    eleb.str = "NO. b";
    INIT_LIST_HEAD(&elec.list);//初始化结构体中的链表指针
    elec.num = 3;
    elec.str = "NO. c";
}

#define print_list_for_each(pos, head, member)         \
	printf("list:%s\n", #head);\
	list_for_each_entry(pos, head, member){\
		printf("num:%d, str:%s\n", pos->num, pos->str);\
	}\
	printf("\n");\

int main(int argc, char *argv[])
{
    element_init();
    LIST_HEAD(head);//双向链表头指针
    list_empty(&head)?printf("list is empty!\n"):printf("list is not empty!\n");

    testlist_s * pos;
    list_add(&ele1.list, &head);//头插入双向链表中  head->ele1
    print_list_for_each(pos, &head, list);
    list_add(&ele2.list, &head);//头插入双向链表中  head->ele2->ele1
    print_list_for_each(pos, &head, list);
    list_add(&ele3.list, &head);//头插入双向链表中  head->ele3->ele2->ele1
    print_list_for_each(pos, &head, list);
    list_add_tail(&ele4.list, &head);//尾插入双向链表中 head->ele3->ele2->ele1->ele4
    print_list_for_each(pos, &head, list);
    list_del(&ele1.list);//删除1元素 head->ele3->ele2->ele4
    print_list_for_each(pos, &head, list);
    list_replace(&ele3.list, &ele5.list);//5替换3  head->ele5->ele2->ele4
    print_list_for_each(pos, &head, list);

    list_empty(&head)?printf("list is empty!\n"):printf("list is not empty!\n");
    list_is_last(&ele4.list, &head)?printf("ele4 is last!\n"):printf("ele4 is not last!\n");
    list_is_last(&ele3.list, &head)?printf("ele3 is last!\n"):printf("ele3 is not last!\n");

    LIST_HEAD(head_new);//双向链表头指针
    list_add_tail(&elea.list, &head_new);
    list_add_tail(&eleb.list, &head_new);
    list_add_tail(&elec.list, &head_new);
    print_list_for_each(pos, &head_new, list);

    list_move(&ele5.list, &head_new);//把5从head中删除，头插到head_new中  head->ele2->ele4   head_new->ele5->elea->eleb->elec
    print_list_for_each(pos, &head, list);
    print_list_for_each(pos, &head_new, list);
    list_move_tail(&eleb.list, &head);//把b从head_new中删除，尾插到head中  head->ele2->ele4->eleb   head_new->ele5->elea->elec
    print_list_for_each(pos, &head, list);
    print_list_for_each(pos, &head_new, list);

    list_splice(&head_new, &head);//合并两个链表到head,新链表头查到head
    print_list_for_each(pos, &head, list);

    list_cut_position(&head_new, &head, &elea.list);//从a分割连个链表
    print_list_for_each(pos, &head, list);
    print_list_for_each(pos, &head_new, list);

    list_splice_tail(&head_new, &head);//合并两个链表到head,新链表尾查到head
    print_list_for_each(pos, &head, list);

    list_for_each_entry_reverse(pos, &head, list){
        printf("num:%d, str:%s\n", pos->num, pos->str);
    }
    printf("\n");

    printf("list len:%d\n", list_len(&head));

    return 0;
}